<?php

namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Request;
use think\Loader;
class Manystore extends Controller{
  	/**
	*商户首页统计  
	**/
	public function statistics(){  
		$mid = input('mid');
		$goods = Db::name('shopro_goods')->where(['store_id'=>$mid])->count();
		$today=strtotime(date('Y-m-d 00:00:00'));
		$data['mid']=$mid;
		$data['finish_time'] = array('egt',$today);
		$data['status'] = array(['=',4],['=',5],'or');
		$order_today = Db::name('order')->where($data)->sum('total_price');
		$order_user = Db::name('order')->where(['mid'=>$mid])->group('user_id')->count();
		$arr = [
			'state'		  =>1,
			'goods'       =>$goods,
			'order_today' =>$order_today,
			'order_user'  =>$order_user
		];
		return json($arr);  
	}

	/**
	*我的资产统计
	**/
	public function asset_statistics(){
		$mid = input('mid');
		$today=strtotime(date('Y-m-d 00:00:00'));
		$data['mid']=$mid;
		$data['finish_time'] = array('egt',$today);
		$order_today = Db::name('order')->where($data)->sum('total_price');
		/*今日收益*/
		$data['mid']=$mid;
		$data['finish_time'] = array('egt',$today);
		$order_today = Db::name('order')->where($data)->sum('total_price');
		/*待入账*/
		$data1['mid']=$mid;
		$data1['finish_time'] = array('egt',$today);
		$data1['status'] = array(['=',3],['=',2],'or');
		$in_an_account = Db::name('order')->where($data1)->sum('total_price');
		/*已入账*/
		$data2['mid']=$mid;  
		$data2['finish_time'] = array('egt',$today);
		$data2['status'] = array(['=',4],['=',5],'or');
		$been_booked = Db::name('order')->where($data2)->sum('total_price');
		/*按月统计*/  
		$where['mid']=$mid;
		$order_month = Db::name('order')->where($where)
          				->field("sum(`total_price`) as month_total_price,FROM_UNIXTIME(finish_time,'%Y-%m') as months")
          				->group('user_id')
          				->limit(12)
          				->order('months desc')
          				->select();
        /*按年统计*/  
		$order_year = Db::name('order')->where($where)
          				->field("sum(`total_price`) as year_total_price,FROM_UNIXTIME(finish_time,'%Y') as months")
          				->group('user_id')
          				->limit(5)
          				->order('months desc')
          				->select();
        /*按日统计*/  
		$order_day = Db::name('order')->where($where)
          				->field("sum(`total_price`) as day_total_price,FROM_UNIXTIME(finish_time,'%Y-%m-%d') as months")
          				->group('user_id')
          				->limit(7)
          				->order('months desc')
          				->select();
      	$arr = [
      		'state'		  	=>1,
      		'order_month'	=>$order_month,
      		'order_year'	=>$order_year,
      		'order_day'		=>$order_day,
      	];

      	return json($arr);
	}
}
